/**
 * Copyright 2009 Sergio Bossa
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.googlecode.actorom.impl.core;

import com.googlecode.actorom.impl.core.reactor.MessageDispatcher;
import com.googlecode.actorom.impl.core.reactor.MessageQueue;
import com.googlecode.actorom.Actor;
import com.googlecode.actorom.Address;
import com.googlecode.actorom.Topology;
import com.googlecode.actorom.support.ActorCreationException;
import java.util.HashMap;
import java.util.Map;
import net.jcip.annotations.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @param <A> 
 * @author Sergio Bossa
 */
@ThreadSafe
public abstract class AbstractTopology<A extends Address> implements Topology<A> {

    private static final transient Logger LOG = LoggerFactory.getLogger(AbstractTopology.class);
    private final Map<A, CoreActor> actors = new HashMap<A, CoreActor>();
    private final MessageQueue messageQueue;
    private final MessageDispatcher messageDispatcher;
    protected volatile boolean shutdown;

    public AbstractTopology(ThreadingPolicy threadingPolicy) {
        messageQueue = new MessageQueue();
        messageDispatcher = threadingPolicy.getMessageDispatcher();
        messageDispatcher.dispatch(messageQueue);
        shutdown = false;
    }

    protected final synchronized boolean doSpawnActor(A address, Object handler) {
        if (!shutdown) {
            if (!actors.containsKey(address)) {
                LOG.info("Spawning actor with address: {}", address);
                CoreProcess process = new CoreProcess(this, address, handler);
                CoreActor actor = new CoreActor(process, messageQueue);
                actors.put(address, actor);
                messageDispatcher.registerHandler(address, process);
                actor.getProcess().spawn();
                return true;
            } else {
                throw new ActorCreationException("Address already assigned to another actor: " + address);
            }
        } else {
            return false;
        }
    }

    protected final synchronized Actor doGetActor(A address) {
        if (!shutdown) {
            CoreActor actor = actors.get(address);
            if (actor != null) {
                if (actor.getProcess().isActive()) {
                    return actor;
                } else {
                    actors.remove(address);
                    messageDispatcher.unregisterHandler(address);
                    return null;
                }
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    protected final synchronized void doShutdown() {
        shutdown = true;
        messageDispatcher.shutdown();
    }
}
